﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Threading;
using System.IO;
using System.Diagnostics;
using System.Xml;
using Mono.Nat;
using Microsoft.PointOfService;

namespace POS
{
    public partial class StartUp : DevExpress.XtraEditors.XtraForm
    {
        Thread StartupProcedures;
        public static StartUp _main;
        

        public StartUp()
        {
            CheckForIllegalCrossThreadCalls = false;
            InitializeComponent();
            _main = this;   
        }

        private void StartUp_Load(object sender, EventArgs e)
        {
            progressPanel1.Description = "";

            StartupProcedures = new Thread(new ThreadStart(Startup1));
            StartupProcedures.Name = "STARTUP THREAD";
            StartupProcedures.Start();

        }

        void Startup1()
        {
            try
            {
                progressPanel1.Caption = "Getting Startup Information";
                Thread.Sleep(250);

                //StaticProperties.StartupStuff.CreateStartupXMLFile();
                StaticProperties.StartupStuff.FileLocation = Environment.CurrentDirectory;

                if (File.Exists(StaticProperties.StartupStuff.FileLocation + @"\" + StaticProperties.StartupStuff.FileName ))
                {
                    XmlDataDocument xmldoc = new XmlDataDocument(); 
                    XmlNodeList xmlnode; 
                    int i = 0; 
                    string str = null;
                    FileStream fs = new FileStream("Startup.xml", FileMode.Open, FileAccess.Read); 
                    xmldoc.Load(fs);
                    xmlnode = xmldoc.GetElementsByTagName("Startup"); 
                    for (i = 0; i <= xmlnode.Count - 1; i++) 
                    { 
                        xmlnode[i].ChildNodes.Item(0).InnerText.Trim();
                        str = xmlnode[i].ChildNodes.Item(0).InnerText.Trim();
                        DataTableClass.SQLServerIP = (str);

                        str = xmlnode[i].ChildNodes.Item(1).InnerText.Trim();
                        DataTableClass.SQLServerPORT = (str);
                    }

                    DataTableClass.myConnectionString = DataTableClass.connectionstring();
                }
                else 
                {
                    SQLServerInfo addinfo = new SQLServerInfo();
                    addinfo.ShowDialog();

                    if(addinfo.DialogResult == System.Windows.Forms.DialogResult.OK)
                    {
                        StaticProperties.StartupStuff.CreateStartupXMLFile(SQLServerInfo._main.lblserverIP.Text, SQLServerInfo._main.lblServerPORT.Text);

                        DataTableClass.SQLServerIP = (SQLServerInfo._main.lblserverIP.Text);
                        DataTableClass.SQLServerPORT = (SQLServerInfo._main.lblServerPORT.Text);

                        DataTableClass.myConnectionString = DataTableClass.connectionstring();
                    }
                }
            }
            catch(Exception E)
            {
                MessageBox.Show(E.Message);
            }

            try
            {
                progressPanel1.Caption = "Getting Network Information";
                try
                {
                    StaticProperties.StartupStuff.theRunningProc = new Process();
                    StaticProperties.StartupStuff.theRunningProc.StartInfo = new ProcessStartInfo();
                    StaticProperties.StartupStuff.theRunningProc.StartInfo.FileName = @"C:\Users\Roger Jenkins\Downloads\Open.NAT-master1\Open.NAT-master\Open.Nat.ConsoleTest\bin\Debug\Open.Nat.ConsoleTest.exe";
                    StaticProperties.StartupStuff.theRunningProc.StartInfo.CreateNoWindow = true;
                    StaticProperties.StartupStuff.theRunningProc.StartInfo.WindowStyle = ProcessWindowStyle.Hidden;
                    //StaticProperties.StartupStuff.theRunningProc.Start();
                    

                    StaticProperties.LocalIPAddress = StaticProperties.GetLocalIP();

                    //Client.ConnectToServer();

                    Thread.Sleep(500);
                    if (DataTableClass.IsServerConnected())
                    {
                        progressPanel1.Caption = "Loading Employee Data";
                        Thread.Sleep(125);
                        DataTableClass.LoadDataTable("EmployeeLogin", DataTableClass.EmployeeLogin);
                        //Thread.Sleep(250);
                        DataTableClass.LoadDataTable("EmployeeInfo", DataTableClass.EmployeeInfo);
                        //Thread.Sleep(250);
                        //DataTableClass.LoadDataTable("EmployeeTimeSheet2", DataTableClass.EmployeeTimeSheet);
                        //Thread.Sleep(250);
                        //DataTableClass.LoadDataTable("JobTitles", DataTableClass.JobTitles);
                        //Thread.Sleep(250);

                        progressPanel1.Caption = "Loading Menu Items";
                        Thread.Sleep(125);
                        DataTableClass.LoadDataTable("MenuCategory", DataTableClass.MenuItemsCategory);
                        DataTableClass.LoadMenuCategoryDictionary();
                        //Thread.Sleep(250);
                        DataTableClass.LoadDataTable("MenuItems", DataTableClass.MenuItems);
                        DataTableClass.LoadMenuItemsDictionary();
                        //Thread.Sleep(250);
                        //DataTableClass.LoadDataTable("ModificationList", DataTableClass.ModificationList);
                        //Thread.Sleep(250);


                        progressPanel1.Caption = "Loading Receipt Data";
                        Thread.Sleep(125);
                        DataTableClass.LoadDataTable("Receipt", DataTableClass.Receipt);
                        //Thread.Sleep(250);
                        DataTableClass.LoadDataTable("ReceiptData", DataTableClass.ReceiptData);
                        //Thread.Sleep(250);
                        //DataTableClass.LoadDataTable("PaymentTypes", DataTableClass.PaymentTypes);
                        //Thread.Sleep(250);
                        //DataTableClass.LoadDataTable("CCPaymentInfo", DataTableClass.CCPaymentInfo);
                        //Thread.Sleep(250);
                        StaticProperties.StartingReceiptNo = 100;
                        //MessageBox.Show(StaticProperties.ReceiptNoGenerator());



                        
                        progressPanel1.Caption = "Loading Table Chart";
                        Thread.Sleep(125);
                        //MessageBox.Show("DONE");
                    }
                    else
                    {
                        throw new ArgumentException("Not able to connect to Database....");
                    }

                }
                catch(Exception E)
                {
                    MessageBox.Show(E.Message);
                }

                try
                {
                    progressPanel1.Description = "Loading Card Reader";

                    PosExplorer myPosExplorer = new PosExplorer();
                    DeviceCollection myDevices;

                    myDevices = myPosExplorer.GetDevices();

                    foreach (DeviceInfo devInfo in myDevices)
                    {
                        //MessageBox.Show(devInfo.ServiceObjectName);

                        if (devInfo.ServiceObjectName == "Microsoft Msr Simulator")
                        {
                            StaticProperties.CreditCardReader = myPosExplorer.CreateInstance(devInfo) as Msr;
                        }
                    }
                }
                catch (Exception EX)
                {
                    MessageBox.Show(EX.ToString());//"UNABLE TO CONNECT TO CARD READER");
                }

                
            }
            catch
            {

            }

            Thread.Sleep(750);
            this.Close();
        }

        private void DeviceFound(object sender, DeviceEventArgs args)
        {
            INatDevice device = args.Device;

            device.CreatePortMap(new Mapping(Protocol.Tcp, 80, 80));

            foreach (Mapping portMap in device.GetAllMappings())
            {
                Console.WriteLine(portMap.ToString());
            }

            Console.WriteLine(device.GetExternalIP().ToString());
        }

        private void DeviceLost(object sender, DeviceEventArgs args)
        {
            INatDevice device = args.Device;

            device.DeletePortMap(new Mapping(Protocol.Tcp, 80, 80));
        }
    }
}
